﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookClubPortal.Models;
using BookClubPortal.ViewModels;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
using System.Web.Configuration;
using WebMatrix.WebData;
using BookClubPortal.Filters;

namespace BookClubPortal.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        BookClubPortalEntities storeDB = new BookClubPortalEntities();

        public ActionResult Index()
        {
            var books = storeDB.Books.Include("Genre").Take(10).ToList();
            return View(books);
        }

        public ActionResult About()
        {
            return View();
        }

        [AuthorizeByDB]
        public ActionResult Contact()
        {
            return View();
        }

        [HttpPost]
        [AuthorizeByDB]
        public ActionResult Contact(EmailContent emailContent)
        {
            if (ModelState.IsValid)
            {
                MvcApplication.SendEmail(MvcApplication.GetApplicationEmailAddress(),
                                            emailContent.Subject,
                                            "Contact message from Book Club from: " + WebSecurity.CurrentUserName + "\n" + emailContent.Message);
            }
            return View(emailContent);
        }
    }
}
